/// <reference path="helpers/_reference.js" />
/// <reference path="templates/pageTpl.js" />
/// <reference path="../WS.EKA.Portal/Scripts/models/page.js" />

describe("Page Model test",function(){
    describe("Page Model init",function(){
    
        it("using defaults attributes when you not set",function(){
            var page = new Page({pageId:"LoginPanel"});

            expect(page.get("pageTitle")).toBe("Wicresoft Mobile");
        });

        it("set attribute when you create instance",function(){
            var page = new Page({pageId:"LoginPanel",pageTitle:"Hello User"});

            expect(page.get("pageId")).toBe("LoginPanel");
            expect(page.get("pageTitle")).toBe("Hello User");
        });
    });

    describe("Trigger change event",function(){

        it("trigger change when you set attribute different",function(){
            var page = new Page({pageId:"LoginPanel"});
            var callback = jasmine.createSpy("changeCallback");
            page.on("change",callback);
            page.set({pageId:"CatalogPanel"});
            expect(callback).toHaveBeenCalled();
        });

        it("not trigger change when you set attribute same",function(){
            var page = new Page({pageId:"LoginPanel"});
            var callback = jasmine.createSpy("changeCallback");
            page.on("change",callback);
            page.set({pageId:"LoginPanel"});
            expect(callback).not.toHaveBeenCalled();
        });
    });

    describe("Trigger error event when your custom validate failed",function(){

        it("pageId must not empty",function(){
            var page = new Page({pageId:"LoginPanel"});
            expect(page).toBeDefined();
            expect(page.get("pageId")).toBeDefined();
        });

        it("pageId is undefined will trigger error",function(){
            var errHandler = jasmine.createSpy("errorCallback");
            var page = new Page({pageId:"LoginPanel"});
            page.on("error",errHandler);

            page.set({pageId:undefined});

            //the arguments pass to errHandler
            var errArgs = errHandler.mostRecentCall.args;

            expect(errHandler).toHaveBeenCalled();
            expect(errArgs).toBeDefined();
            
            //trigger error will return Model,Error to handler
            expect(errArgs[0]).toBe(page);
            expect(errArgs[1].message||errArgs[1]).toBe("You must set id of your page [pageId]");
        });
    });
});

describe("Page View Test",function(){
    
    //add template for test
    $("head").append(panel_tpl).append(actionPanel).append(processBar)
             .append(panelContent_tpl).append(header_tpl);
    
    window.pageRoute = new PageRoute();

    beforeEach(function(){
        this.pageModel = new Page({ 
            pageId: "LoginPanel", pageTitle: "Wicresoft Mobile",
            menuTitle: "Please Login", headerId: "login_header", logout: false, back: false, action: false,
            innerDivId: "Login", firstLoad: false
        });
        this.pageView = new PageView({model:this.pageModel});

    });

    describe("Page View init",function(){
        it("pageView default el is body",function(){
            expect(this.pageView.el).toBe("body");
        });

        it("data in View is from its Model(the parameter 'model' you pass)",function(){
            expect(this.pageView.model).toBeDefined();
            expect(this.pageView.model).toEqual(this.pageModel);
        });

        it("init processbar hidden",function(){
            expect($(".UpdateProgressBar")).toExist();
            expect($(".UpdateProgressBar")).toBeHidden();
        });

        it("init actionPanel hidden",function(){
            expect($(".operateBar")).toExist();
            expect($(".operateBar")).toBeHidden();
        });

    });

    describe("Page View render",function(){

        it("render should return this view",function(){
            expect(this.pageView.render()).toEqual(this.pageView);
        });

        it("render a div whose id equal to your pageId into #content",function(){
            expect($("#content")).toContain($("#"+this.pageModel.get('pageId')));
            expect($("#"+this.pageModel.get('pageId'))).toHaveClass('panel');
        });

        it("register iScroll after render 500ms",function(){
            spyOn(this.pageView,"registerScroll");
            this.pageView.render();
            expect(this.pageView.registerScroll).toHaveBeenCalled();

            //only can get myScroll after 500ms
            expect((this.pageView.myScroll) instanceof iScroll).not.toBeTruthy();

            window.setTimeout(function(){
                expect((this.pageView.myScroll) instanceof iScroll).toBeTruthy();
            },500);
            
        });

        it("every time render page must set route",function(){
            spyOn(pageRoute,"navigate");
            this.pageView.render();
            expect(pageRoute.navigate).toHaveBeenCalled();
            //using "page/#pageId" to set url
            expect(pageRoute.navigate).toHaveBeenCalledWith('page/' + this.pageModel.get("pageId"));
        });

        afterEach(function(){
            $("#jQUi").hide();
        });
        
    });

    describe("Page inner function",function(){
        it("display action panel function--displayActionPanel",function(){
            this.pageView.displayActionPanel();
            expect($(".operateBar")).toBeVisible();
        });

        it("hide action panel function--hideActionPanel",function(){
            this.pageView.displayActionPanel();
            this.pageView.hideActionPanel();
            expect($(".operateBar")).toBeHidden();
        });

        it("display process bar function--displayProcessBar",function(){
           this.pageView.displayProcessBar();
           expect($(".UpdateProgressBar")).toBeVisible(); 
        });

        it("hide process bar function--hideProcessBar",function(){
            this.pageView.displayProcessBar();
            this.pageView.hideProcessBar();
            expect($(".UpdateProgressBar")).toBeHidden(); 
        });
    });

    afterEach(function(){
        $(".operateBar").remove();$(".UpdateProgressBar").remove();
    });

    
});

